<?php

namespace App\Dao;

use App\Models\TicketOrderRefundLog;
use App\Utilities\Alipay\AopClient;
use App\Utilities\Alipay\Request\AlipayTradeRefundRequest;
use EasyWeChat\Factory;
use Illuminate\Database\Eloquent\Model;

class PaymentDao
{

	/**
	 * 微信退款
	 *
	 * @author lxp 20181025
	 * @param array $payment_info 支付信息数组
	 * @param array $params 业务数据数组
	 * @param Model $refundModel
	 * @return bool|string
	 * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
	 */
	public static function wxpay_refund($payment_info, $params, Model $refundModel = null)
	{
		$wxapp = Factory::payment([
			'app_id' => $payment_info['wxAppId'],
			'mch_id' => $payment_info['wxMchId'],
			'key' => $payment_info['wxkey'],
			'cert_path' => $payment_info['cert_path'],
			'key_path' => $payment_info['key_path']
		]);

		$result = $wxapp->refund->byOutTradeNumber($donate['order_sn'], $donate['order_sn'], intval($donate['money'] * 100), intval($donate['money'] * 100));

		// 记录同步通知日志
		$logObj = app('logext');
		$logObj->init('wxpay_refund');
		$logObj->logbuffer('reponse', $result);
		$logObj->logend();

		// 保存同步返回结果
		if ($refundModel instanceof TicketOrderRefundLog) {
			if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
				$refundModel->refund_status = 2;
			} else {
				$refundModel->refund_status = 5;
			}
			$refundModel->refund_response = json_encode($result);
			$refundModel->save();
		}

		if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
			return true;
		} else {
			return '退款请求失败，请稍后重试';
		}
	}

	/**
	 * 支付宝退款
	 *
	 * @author lxp 20181025
	 * @param array $payment_info 支付信息数组
	 * @param array $params 业务数据数组
	 * @param Model $refundModel
	 * @return bool|string
	 */
	public static function alipay_refund($payment_info, $params, Model $refundModel = null)
	{
		$alipay = new AopClient();
		$alipay->appId = $payment_info['appId'];
		$alipay->signType = 'RSA2';
		$alipay->rsaPrivateKey = file_get_contents($payment_info['private_key_path']);
		$alipay->alipayrsaPublicKey = file_get_contents($payment_info['public_key_path']);

		$alipay_request = new AlipayTradeRefundRequest();
		// 支付参数
		$alipay_request->setBizContent(json_encode([
			// 外部订单号
			'out_trade_no' => $params['out_trade_no'],
			// 本次退款请求流水号，部分退款时必传
			'out_request_no' => $params['out_request_no'],
			// 退款金额
			'refund_amount' => $params['refund_amount']
		]));
		$r = $alipay->execute($alipay_request);
		$r_data = json_decode(json_encode($r), true);
		$response = isset($r_data['alipay_trade_refund_response']) ? $r_data['alipay_trade_refund_response'] : [];

		// 记录同步通知日志
		$logObj = app('logext');
		$logObj->init('alipay_refund');
		$logObj->logbuffer('reponse', $r_data);
		$logObj->logend();

		// 保存同步返回结果
		if ($refundModel instanceof TicketOrderRefundLog) {
			$refundModel->refund_response = json_encode($r_data);
			$refundModel->save();
		}

		if (isset($response['code']) && $response['code'] == '10000') {
			// 同步通知成功
			return true;
		} else {
			// 同步通知失败
			return '退款请求失败，请稍后重试';
		}
	}
}
